package com.yilong.wei.app.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yilong.wei.app.config.UploadFileConfig;
import com.yilong.wei.app.domain.Country;
import com.yilong.wei.app.domain.FileDO;
import com.yilong.wei.app.domain.Respond;
import com.yilong.wei.app.service.FileService;
import com.yilong.wei.app.utils.FileType;
import com.yilong.wei.app.utils.LocalDateTimeUtils;
import org.aspectj.weaver.ast.Var;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("layui")
public class LayuiController {

    private static final Logger logger = LoggerFactory.getLogger(CountryController.class);

    @Autowired
    private UploadFileConfig uploadFileConfig;

    @Autowired
    private FileService fileService;

    @RequestMapping("upload")
    public String interUpload(){

        return "layui/upload";
    }

    @RequestMapping("fileList")
    public String fileList(){

        return "layui/filelist";
    }

    @RequestMapping(value = "/getList")
    @ResponseBody
    public Respond list(HttpServletRequest request){
        Respond respond = new Respond();
        String page = request.getParameter("page");
        String limit = request.getParameter("limit");
        String id = request.getParameter("id");
        String type = request.getParameter("type");
        String url = request.getParameter("url");
        String createDate = request.getParameter("createDate");
        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));
        Map map = new HashMap(16);
        map.put("id",id);
        map.put("type",type);
        map.put("url",url);
        map.put("createDate",createDate);
        List<FileDO> list = fileService.findFileByMap(map);
        PageInfo<FileDO> pageInfo = new PageInfo(list);
        logger.info("执行方法:{},执行时间:{}","getList", LocalDateTimeUtils.getNowTime());
//        return JSONObject.toJSONString(pageInfo);
        List<FileDO> pageInfoList = pageInfo.getList();
//        valueOperations.set(key,pageInfo,30, TimeUnit.MINUTES);
        respond.setCode(200);
        respond.setMsg("请求成功...");
        respond.setCount(pageInfo == null ? 0 : (int)pageInfo.getTotal());
        respond.setData(pageInfoList);
        return respond;
    }

    @RequestMapping("uploadFile")
    @ResponseBody
    public Respond uploadFile(@RequestParam("file") MultipartFile file, HttpServletRequest request){
        Respond respond = new Respond();
        try {
            String dateTime = LocalDateTimeUtils.formatDateTime(LocalDateTime.now(), LocalDateTimeUtils.DATE_PATTERN_FILE);
//            String realPath = request.getSession().getServletContext().getRealPath("/uploadFile/");
            //创建需要放置的文件夹
            String realPath = uploadFileConfig.getUploadPath();
            File folder = new File(realPath+dateTime);
            if (!folder.isDirectory()){
                folder.mkdirs();
            }
            //获取接受的文件名和文件
            String oldFileName = file.getOriginalFilename();
            String newFileName = UUID.randomUUID().toString() + oldFileName.substring(oldFileName.lastIndexOf("."));
            file.transferTo(new File(folder,newFileName));
//            String filePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() +
//                    "/uploadFile/" + dateTime + newFileName;
            String filePath = uploadFileConfig.getBasePath()+dateTime+File.separatorChar+newFileName;
            //创建文件
            FileDO sysFile = new FileDO(FileType.fileType(newFileName), filePath, new Date());
//            保存文件
            if (fileService.save(sysFile) >0) {
                respond.setCode(200);
                respond.setMsg("上传成功");
                respond.setData(filePath);
            }
        } catch (IOException e) {
            respond.setCode(500);
            respond.setMsg("上传失败");
        }
        return respond;
    }
}
